﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Application.Admin.Sight
{
    public class SightPointService : ISightPointService
    {
        Domain.Uow.IRepository<Domain.Entity.SightPoint> sightRepository;

        Domain.Uow.IRepository<Domain.Entity.SightPointVersion> versionRepository;

        public SightPointService(Domain.Uow.IRepository<Domain.Entity.SightPoint> sight,
            Domain.Uow.IRepository<Domain.Entity.SightPointVersion> sightVersion)
        {
            if (sight == null)
            {
                throw new ArgumentNullException("sightRepository is null");
            }
            if (sightVersion == null)
            {
                throw new ArgumentNullException("sightVersionRepository is null");
            }
            sightRepository = sight;
            versionRepository = sightVersion;
        }

        public Domain.Entity.SightPoint Add(Domain.Entity.SightPoint add, Domain.Entity.User operUser)
        {
            if (add == null) return add;
            sightRepository.Add(add);
            sightRepository.Uow.Commit();
            UpdateSightVersion();
            return add;
        }


        public bool Delete(Domain.Entity.SightPoint delete, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                delete.State = false;
                sightRepository.Modify(delete);
                sightRepository.Uow.Commit();
                res = true;
                UpdateSightVersion();
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(Domain.Entity.SightPoint delete, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                sightRepository.Delete(delete);
                sightRepository.Uow.Commit();
                res = true;
                UpdateSightVersion();
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Delete(IList<int> idList, Domain.Entity.User operUser)
        {
            var res = false;
            var delete = sightRepository.GetList(e => idList.Contains(e.Id));
            foreach (var item in delete)
            {
                item.State = false;
                sightRepository.Modify(item);
            }
            try
            {
                sightRepository.Uow.Commit();
                res = true;
                UpdateSightVersion();
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(IList<int> idList, Domain.Entity.User operUser)
        {
            var res = false;
            var delete = sightRepository.GetList(e => idList.Contains(e.Id));
            foreach (var item in delete)
            {
                sightRepository.Delete(item);
            }
            try
            {
                sightRepository.Uow.Commit();
                res = true;
                UpdateSightVersion();
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Modify(Domain.Entity.SightPoint modify, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                sightRepository.Modify(modify);
                sightRepository.Uow.Commit();
                res = true;
                UpdateSightVersion();
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public Domain.Entity.SightPoint Get(int id)
        {
            var data = sightRepository.Get(id);
            return data;
        }

        public IQueryable<Domain.Entity.SightPoint> GetList()
        {
            var data = sightRepository.GetList();
            return data;
        }


        #region util

        protected void UpdateSightVersion()
        {
            var sightVersion = versionRepository.GetList().FirstOrDefault();
            if (sightVersion == null)
            {
                sightVersion = new Domain.Entity.SightPointVersion();
                sightVersion.AddTime = System.DateTime.Now;
                sightVersion.IpAddress = Infrastructure.Crosscutting.Comm.Service.StringService.GetRealIP();
                sightVersion.SortNum = 0;
                sightVersion.State = true;
                sightVersion.Version = 1;
                versionRepository.Add(sightVersion);
                versionRepository.Uow.Commit();
            }
            else
            {
                sightVersion.Version += 1;
                versionRepository.Uow.Commit();
            }
        }

        #endregion

    }
}
